Imports LookUp
Imports System.Data
Imports System.IO

Partial Class WebForms_ClientInfo
    Inherits WebFormCommon

    Private strPath As String = ""

#Region "Page Security"
    Private Sub SecurePage()
        Dim sec As New Secure

        With sec
            'Delete
            .AddDataGridColumn(CInfo_dg, CInfo_dg.Columns.Count - 4, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Rider_dg, Rider_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Fund_dg, Fund_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Beneficiary_dg, Beneficiary_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Trustee_dg, Trustee_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Advisor_dg, Advisor_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Issue_dg, Issue_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Attachment_dg, Attachment_dg.Columns.Count - 1, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(Referral_dg, Referral_dg.Columns.Count - 2, Secure.ControlTypes.DeleteType)

            .AddDataGridColumn(CInfo_dg, CInfo_dg.Columns.Count - 5, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Rider_dg, Rider_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Fund_dg, Fund_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Beneficiary_dg, Beneficiary_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Trustee_dg, Trustee_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Advisor_dg, Advisor_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Issue_dg, Issue_dg.Columns.Count - 3, Secure.ControlTypes.EditType)
            .AddDataGridColumn(Referral_dg, Referral_dg.Columns.Count - 3, Secure.ControlTypes.DeleteType)

            .AddControl(AddNewBusiness, Secure.ControlTypes.AddType)
            .AddControl(AddRider_btn, Secure.ControlTypes.AddType)
            .AddControl(AddFund_btn, Secure.ControlTypes.AddType)
            .AddControl(AddBeneficiary_btn, Secure.ControlTypes.AddType)
            .AddControl(AddTrustee_btn, Secure.ControlTypes.AddType)
            .AddControl(AddAdvisor_btn, Secure.ControlTypes.AddType)
            .AddControl(AddIssue_btn, Secure.ControlTypes.AddType)
            .AddControl(AddAttachment_btn, Secure.ControlTypes.AddType)
            .AddControl(divAttachPop, Secure.ControlTypes.AddType)
            .AddControl(AddReferral_btn, Secure.ControlTypes.AddType)

            .AddControl(Save_btn, Secure.ControlTypes.EditType)

            .ParentPage = ParentPage
            .SetPageAccess(Session("UserName"))


        End With

        sec = Nothing
    End Sub

    Private Sub FormDisable()
        Select Case ViewState("mode")
            Case "view"
                Dim cf As New CIMSfunc
                With cf
                    .AddExemptControl(Client_btn)
                    .AddExemptControl(Riders_btn)
                    .AddExemptControl(Funds_btn)
                    .AddExemptControl(Beneficiaries_btn)
                    .AddExemptControl(Trustee_btn)
                    .AddExemptControl(Advisor_btn)
                    .AddExemptControl(Issues_btn)

                    .FormDisable(divMain)
                End With

                AddNewBusiness.Visible = False
                AddRider_btn.Visible = False
                AddFund_btn.Visible = False
                AddBeneficiary_btn.Visible = False
                AddTrustee_btn.Visible = False
                AddAdvisor_btn.Visible = False
                AddIssue_btn.Visible = False
                AddReferral_btn.Visible = False

                Submit_btn.Visible = False

                CInfo_dg.Columns(CInfo_dg.Columns.Count - 5).Visible = False
                Rider_dg.Columns(Rider_dg.Columns.Count - 3).Visible = False
                Fund_dg.Columns(Fund_dg.Columns.Count - 3).Visible = False
                Beneficiary_dg.Columns(Beneficiary_dg.Columns.Count - 3).Visible = False
                Trustee_dg.Columns(Trustee_dg.Columns.Count - 3).Visible = False
                Advisor_dg.Columns(Advisor_dg.Columns.Count - 3).Visible = False
                Issue_dg.Columns(Issue_dg.Columns.Count - 3).Visible = False
                Referral_dg.Columns(Referral_dg.Columns.Count - 3).Visible = False

                CInfo_dg.Columns(CInfo_dg.Columns.Count - 4).Visible = False
                Rider_dg.Columns(Rider_dg.Columns.Count - 2).Visible = False
                Fund_dg.Columns(Fund_dg.Columns.Count - 2).Visible = False
                Beneficiary_dg.Columns(Beneficiary_dg.Columns.Count - 2).Visible = False
                Trustee_dg.Columns(Trustee_dg.Columns.Count - 2).Visible = False
                Advisor_dg.Columns(Advisor_dg.Columns.Count - 2).Visible = False
                Issue_dg.Columns(Issue_dg.Columns.Count - 2).Visible = False
                Referral_dg.Columns(Referral_dg.Columns.Count - 2).Visible = False

                cf = Nothing
            Case "edit"
                CInfo_dg.Columns(CInfo_dg.Columns.Count - 3).Visible = False
                Rider_dg.Columns(Rider_dg.Columns.Count - 1).Visible = False
                Fund_dg.Columns(Fund_dg.Columns.Count - 1).Visible = False
                Beneficiary_dg.Columns(Beneficiary_dg.Columns.Count - 1).Visible = False
                Trustee_dg.Columns(Trustee_dg.Columns.Count - 1).Visible = False
                Advisor_dg.Columns(Advisor_dg.Columns.Count - 1).Visible = False
                Issue_dg.Columns(Issue_dg.Columns.Count - 1).Visible = False
                Referral_dg.Columns(Referral_dg.Columns.Count - 1).Visible = False
        End Select

    End Sub
#End Region

#Region "Client Info Code"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cf As New CIMSfunc

        cf.setNumericFormat(ContributionAmount_txt)
        cf.setNumericFormat(InsuredAmount_txt)
        cf.setNumericFormat(VanishingInterest_txt)
        cf.setNumericFormat(VanishingYear_txt)

        If Not Page.IsPostBack Then
            Dim lookup As New FillLookUp

            Call DetailLinkClick()

            Call SecurePage()
            With lookup

                .FillDropDown(PlanType_cmb, FillLookUp.eLookUpType.other, "PLAN_TYPE")
                .FillDropDown(Provider_cmb, FillLookUp.eLookUpType.provider)
                .FillDropDown(Product_cmb, FillLookUp.eLookUpType.prdt, , Provider_cmb.SelectedValue.ToString)
                .FillDropDown(PolicyStatus_cmb, FillLookUp.eLookUpType.other, "POLICY_STATUS")
                .FillDropDown(ClientType_cmb, FillLookUp.eLookUpType.other, "CLIENT_TYPE")
                .FillDropDown(Medical_Rating_cmb, FillLookUp.eLookUpType.other, "MEDICAL_RATING")
                .FillDropDown(Currency_cmb, FillLookUp.eLookUpType.other, "CURRENCY")
                .FillDropDown(FrequencyOfPayment_cmb, FillLookUp.eLookUpType.frequency)

            End With

            lookup = Nothing

            If Not IsNothing(Session("Seq_CD")) Then
                lblID.Text = Session("Seq_CD")
                ViewState.Add("Seq_CD", Session("Seq_CD"))
                LoadPolicyInfo()
                GetFundTotals()
                GetBeneficiaryTotals()
            Else
                ContributionAmount_txt.Text = "0"
            End If

            If Not IsNothing(Session("PolicyNo")) Then
                Policy_lbl.Text = Session("PolicyNo")
                ViewState.Add("PolicyNo", Session("PolicyNo"))
                If ViewState("PolicyNo") = "&nbsp;" Then ViewState("PolicyNo") = ""
            End If

            If Not IsNothing(Session("Provider")) Then
                Provider_lbl.Text = Session("Provider")
                ViewState.Add("Provider", Session("Provider"))
            End If

            ViewState.Add("UserName", Session("UserName"))

            If Not IsNothing(Session("mode")) Then ViewState.Add("mode", Session("mode"))
            If Not IsNothing(Session("pagesource")) Then ViewState.Add("pagesource", Session("pagesource"))
            If Not IsNothing(Session("advisor")) Then ViewState.Add("advisor", Session("advisor"))

            Session.Clear()
            Session.Add("UserName", ViewState("UserName"))
            Session.Add("advisor", ViewState("advisor"))

            If Not IsNothing(ViewState("Seq_CD")) Then
                LoadPolicyInfo()
                GetFundTotals()
                GetBeneficiaryTotals()
            Else
                ContributionAmount_txt.Text = "0"
            End If

            lblID.Text = ViewState("Seq_CD")
            Policy_lbl.Text = ViewState("Provider")
            Call FormDisable()

            Dim dm As New DAOMain.CommonDAO
            Dim dt As DataTable = dm.ExecQuery("Select dbo.fn_GetPolicyName(" & lblID.Text & ") as mName")

            If dt.Rows.Count > 0 Then
                tdName.InnerText = dt.Rows(0)("mName").ToString
            End If

            'dt = dm.ExecQuery("Select 'ADVISOR :  ' + dbo.fn_GetAdvisorName(" & lblID.Text & ") as Advisor_Name")
            dt = dm.ExecQuery("Select dbo.fn_GetAdvisorName(" & lblID.Text & ") as Advisor_Name")

            If dt.Rows.Count > 0 Then
                Me.tdAdvisor.InnerText = dt.Rows(0)("Advisor_Name").ToString
            End If

            dm = Nothing
        End If

        Select Case Me.PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
                Referral_lbl.Visible = False
                Referrals_btn.Visible = False

                Payment_lbl.Visible = False
                Payment_btn.Visible = False

                If lblID.Text = "" Then
                    divClientDetails.Visible = False
                    Submit_btn.Visible = False
                Else
                    divClientDetails.Visible = True
                    If ViewState("mode") <> "view" Then
                        Submit_btn.Visible = True
                    End If
                End If
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
                Submit_btn.Visible = False
        End Select
    End Sub

    Private Sub GetFundTotals()
        Dim dm As New DAOMain.CommonDAO

        dm.AddParameter("Fund")
        dm.AddParameter(ViewState("Seq_CD"))
        dm.AddParameter("0")
        dm.AddParameter("")
        dm.AddParameter("")

        Dim dt As DataTable = dm.ExecQryProc("sp_get_total_percent")

        If dt.Rows.Count > 0 Then
            If Val(dt.Rows(0).Item("Percentage")) < 100 Then
                FundTotal_lbl.Text = "Total % : " & dt.Rows(0).Item("Percentage") & " %"
            End If
        End If

        dm = Nothing
    End Sub

    Private Sub GetBeneficiaryTotals()
        Dim dao As New DAOMain.CommonDAO
        Dim sb As New StringBuilder

        Dim dtRanking As DataTable = _
                dao.ExecQuery("Select distinct ranking_cd From policy_bene_dtl Where policy_seq_cd = " & dao.Q(ViewState("Seq_CD")))

        Dim i As Integer

        For i = 0 To dtRanking.Rows.Count - 1
            Dim dm As New DAOMain.CommonDAO

            dm.AddParameter("Beneficiary")
            dm.AddParameter(ViewState("Seq_CD"))
            dm.AddParameter("0")
            dm.AddParameter("")
            dm.AddParameter(dtRanking.Rows(i)("ranking_cd"))

            Dim dt As DataTable = dm.ExecQryProc("sp_get_total_percent")

            If dt.Rows.Count > 0 Then
                If Val(dt.Rows(0).Item("Percentage")) < 100 Then
                    sb.Append(GetRankingName(dtRanking.Rows(i)("ranking_cd")) & " : " & dt.Rows(0).Item("Percentage") & " % ")
                End If
            End If

            dm = Nothing
            dt = Nothing
        Next

        BeneficiaryTotal_lbl.Text = sb.ToString

        dao = Nothing
        sb = Nothing
        dtRanking = Nothing
    End Sub

    Private Function GetRankingName(ByVal ranking_cd As String) As String
        Dim dm As New DAOMain.CommonDAO
        Dim strReturn As String = ""

        Dim dt As DataTable = dm.ExecQuery("Select Lookup_Desc From lookup_mstr Where lookup_seq = " & dm.Q(ranking_cd))

        If dt.Rows.Count > 0 Then
            strReturn = dt.Rows(0)("Lookup_Desc")
        End If

        dm = Nothing
        dt = Nothing

        Return strReturn
    End Function

    Protected Sub Provider_cmb_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Provider_cmb.SelectedIndexChanged
        Dim lookup As New LookUp.FillLookUp
        lookup.FillDropDown(Product_cmb, FillLookUp.eLookUpType.prdt, , Provider_cmb.SelectedValue.ToString)
        lookup = Nothing
    End Sub

    Private Sub LoadPolicyInfo()
        'On Error Resume Next

        Dim vo As New DAOMain.POLICY_TRANS_VO
        Dim dm As New DAOMain.POLICY_TRANS_DAO
        Dim cFunc As New CIMSfunc
        Dim lookup As New LookUp.FillLookUp

        vo.POLICY_SEQ_CD = lblID.Text
        Dim dt As DataTable = dm.Search(vo)

        If dt.Rows.Count > 0 Then
            With dt.Rows(0)
                Client_Notes_txt.Text = .Item("CLIENT_NOTE")
                ClientType_cmb.SelectedValue = .Item("CLIENT_TYPE")
                CommenceAge_txt.Text = .Item("COMMENCE_AGE")
                ContributionAmount_txt.Text = Format(.Item("CONTRIBUTION_AMT"), "###,###,##0.00")

                Currency_cmb.SelectedValue = .Item("CURRENCY_CD")
                Establishment_txt.Text = .Item("ESTABLISH_PERIOD")
                FrequencyOfPayment_cmb.SelectedValue = .Item("FREQUENCY_CD")
                InsuredAmount_txt.Text = Format(.Item("INSURED_AMT"), "###,###,##0.00")
                Medical_Rating_cmb.SelectedValue = .Item("MEDICAL_RATE_CD")
                MedicalPercent_txt.Text = .Item("MEDICAL_RATE_PER")
                Other_Requirements_txt.Text = .Item("OTHERS_REQ")
                Currency_cmb.SelectedValue = .Item("PAYMENT_CURR")
                PlanType_cmb.SelectedValue = .Item("PLAN_TYPE")

                ContributionDueDate_txt.Text = cFunc.ParseGetDate(.Item("CONTRIBUTION_DUE_DT"))
                ApprovalDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_APPROVAL_DT"))
                EffectivityDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_EFF_DT"))
                PolicyIssueDate_txt.Text = cFunc.ParseGetDate(.Item("POLICY_ISSUE_DT"))

                PolicyNumber_txt.Text = .Item("POLICY_NO")
                PolicyStatus_cmb.SelectedValue = .Item("POLICY_ST")
                Provider_cmb.SelectedValue = .Item("PROVIDER_CD")
                lookup.FillDropDown(Product_cmb, FillLookUp.eLookUpType.prdt, , Provider_cmb.SelectedValue.ToString)

                Product_cmb.SelectedValue = .Item("PRDT_CD")
                VanishingInterest_txt.Text = .Item("VANISH_PER")
                VanishingYear_txt.Text = .Item("VANISH_YRS")
                PolicyRemarks_txt.Text = .Item("POLICY_REMARKS")
            End With
        End If

        CInfo_dg.CurrentPageIndex = 0
        Rider_dg.CurrentPageIndex = 0
        Fund_dg.CurrentPageIndex = 0
        Beneficiary_dg.CurrentPageIndex = 0
        Trustee_dg.CurrentPageIndex = 0
        Advisor_dg.CurrentPageIndex = 0
        Issue_dg.CurrentPageIndex = 0
        Referral_dg.CurrentPageIndex = 0
        Attachment_dg.CurrentPageIndex = 0
        Payment_dg.CurrentPageIndex = 0

        Call Load_CInfo()
        Call Load_Bene_Dtl()
        Call Load_Trustee_Dtl()
        Call Load_Rider_Dtl()
        Call Load_Fund_Dtl()
        Call Load_Adv_Dtl()
        Call Load_Issue_Dtl()
        Call Load_Referrals()
        Call Load_Payments()
        Call Load_Attachments()

        lookup = Nothing
        cFunc = Nothing
        vo = Nothing
        dm = Nothing
    End Sub

    Protected Sub Provider_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lookup As New LookUp.FillLookUp

        lookup.FillDropDown(Product_cmb, FillLookUp.eLookUpType.prdt, , Provider_cmb.SelectedValue.ToString)

        lookup = Nothing
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        If CheckFrequency() Then
            Call DoSave()
        Else
            Dim pFunc As New PolyFunc.ASPFunc

            pFunc.MessageAlert(ParentPage, "Please complete one year of payment before changing the payment frequency")
            pFunc = Nothing
        End If

    End Sub

    Private Sub DoSave()
        Dim vo As New DAOMain.POLICY_TRANS_VO
        Dim dm As New DAOMain.POLICY_TRANS_DAO
        Dim polyFunc As New PolyFunc.ASPFunc
        Dim cFUnc As New CIMSfunc

        With vo
            If lblID.Text = "" Then
                Dim dt As DataTable = dm.ExecQuery("Select isnull(max(convert(numeric, policy_seq_cd)),0) +1 as Seq_CD From policy_trans")

                .POLICY_SEQ_CD = dt.Rows(0)("Seq_CD")
            Else
                .POLICY_SEQ_CD = lblID.Text
            End If

            Dim dtBroker As DataTable = dm.ExecQuery("Select broker_cd From provider_mstr Where provider_cd = " & dm.Q(Provider_cmb.SelectedValue.ToString))

            .BROKER_CD = dtBroker.Rows(0)("broker_cd")
            .CLIENT_NOTE = Client_Notes_txt.Text
            .CLIENT_TYPE = ClientType_cmb.SelectedValue.ToString
            .COMMENCE_AGE = Val(CommenceAge_txt.Text)
            .CONTRIBUTION_AMT = cFUnc.saveFormat(ContributionAmount_txt.Text)
            .CURRENCY_CD = Currency_cmb.SelectedValue.ToString
            .ESTABLISH_PERIOD = Val(Establishment_txt.Text)
            .FREQUENCY_CD = FrequencyOfPayment_cmb.SelectedValue.ToString
            .INSURED_AMT = cFUnc.saveFormat(InsuredAmount_txt.Text)
            .MEDICAL_RATE_CD = Medical_Rating_cmb.SelectedValue.ToString
            .MEDICAL_RATE_PER = Val(MedicalPercent_txt.Text)
            .OTHERS_REQ = Other_Requirements_txt.Text
            .PAYMENT_CURR = Currency_cmb.SelectedValue.ToString
            .PLAN_TYPE = PlanType_cmb.SelectedValue.ToString

            .CONTRIBUTION_DUE_DT = cFUnc.ParseSetDate(ContributionDueDate_txt.Text)
            .POLICY_APPROVAL_DT = cFUnc.ParseSetDate(ApprovalDate_txt.Text)
            .POLICY_EFF_DT = cFUnc.ParseSetDate(EffectivityDate_txt.Text)
            .POLICY_ISSUE_DT = cFUnc.ParseSetDate(PolicyIssueDate_txt.Text)

            .POLICY_NO = PolicyNumber_txt.Text
            .POLICY_ST = PolicyStatus_cmb.SelectedValue.ToString
            .PRDT_CD = Product_cmb.SelectedValue.ToString
            .POLICY_REMARKS = PolicyRemarks_txt.Text

            Select Case Me.PageControlMode
                Case ControlMode.NewBusiness
                    .PROCESS_ST = "NB"
                Case ControlMode.ClientServicing
                    .PROCESS_ST = "CS"
            End Select

            .PROVIDER_CD = Provider_cmb.SelectedValue.ToString
            .VANISH_PER = Val(VanishingInterest_txt.Text)
            .VANISH_YRS = Val(VanishingYear_txt.Text)

        End With

        If lblID.Text = "" Then
            vo.POLICY_CREA_BY = Session("UserName")
            vo.POLICY_CREA_DT = Now

            If dm.Create_Record(vo) Then
                polyFunc.MessageAlert(ParentPage, "Record Saved")
                lblID.Text = vo.POLICY_SEQ_CD

                ViewState.Add("Seq_CD", vo.POLICY_SEQ_CD)
                ViewState.Add("PolicyNo", vo.POLICY_NO)
                ViewState.Add("Provider", Provider_cmb.SelectedItem.Text & " - " & Product_cmb.SelectedItem.Text)

                Call LoadPolicyInfo()
                Policy_lbl.Text = vo.POLICY_NO
                Provider_lbl.Text = Provider_cmb.SelectedItem.Text & " - " & Product_cmb.SelectedItem.Text
                divClientDetails.Visible = True
                Submit_btn.Visible = True
            Else
                polyFunc.MessageAlert(ParentPage, "Error Saving Record")
            End If
        Else
            vo.POLICY_LAST_UPD_BY = Session("UserName")
            vo.POLICY_LAST_UPD_DT = Now

            If dm.Update(vo) Then
                polyFunc.MessageAlert(ParentPage, "Record Saved")
                Call LoadPolicyInfo()
            Else
                polyFunc.MessageAlert(ParentPage, "Error Saving Record")
            End If
        End If

        cFUnc = Nothing
        polyFunc = Nothing
        vo = Nothing
        dm = Nothing
    End Sub

    Private Function CheckFrequency() As Boolean
        Dim blnReturn As Boolean = True

        If lblID.Text <> "" Then
            Dim dm As New DAOMain.CommonDAO
            Dim cnt As Integer
            Dim dt As New DataTable

            dt = dm.ExecQuery("Select frequency_cd From policy_trans Where policy_seq_cd = " & dm.Q(lblID.Text))
            If dt.Rows.Count > 0 Then
                If Me.FrequencyOfPayment_cmb.SelectedValue <> dt.Rows(0)("frequency_cd") Then
                    'compare old and new freq count
                    If GetFreqCount(FrequencyOfPayment_cmb.SelectedValue) > GetFreqCount(dt.Rows(0)("frequency_cd")) Then
                        dt = dm.ExecQuery("Select isnull(sum(freq_cnt), 0) as cnt From policy_premium_dtl Where pay_tag = 1 And policy_seq_cd = " & dm.Q(lblID.Text))

                        If dt.Rows.Count > 0 Then
                            If dt.Rows(0).Item("cnt") <> 0 Then
                                cnt = dt.Rows(0).Item("cnt")

                                If cnt Mod 12 <> 0 Then
                                    blnReturn = False
                                End If
                            End If
                        End If

                    End If
                End If
            End If
        End If

        Return blnReturn
    End Function

    Private Function GetFreqCount(ByVal freq_cd As String) As Integer
        Dim blnReturn As Integer = 0

        Dim dao As New DAOAdmin.FREQUENCY_MSTR_DAO
        Dim vo As New DAOAdmin.FREQUENCY_MSTR_VO

        vo.FREQUENCY_CD = freq_cd
        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then blnReturn = dt.Rows(0)("FREQUENCY_COUNT")

        dao = Nothing
        vo = Nothing

        Return blnReturn
    End Function

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        If IsNothing(ViewState("pagesource")) Then
            Response.Redirect("~/" & strPath & "/Search_Policies.aspx")
        Else
            Response.Redirect(ViewState("pagesource"))
        End If

    End Sub

    Protected Sub Submit_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit_btn.Click
        Dim dm As New DAOMain.CommonDAO
        Dim polyFunc As New PolyFunc.ASPFunc

        dm.AddParameter(lblID.Text)
        dm.AddParameter("CS")

        If dm.ExecCmdProc("sp_set_policy_trans_stat") Then
            polyFunc.MessageAlert(ParentPage, "Policy Sent to Client Servicing!")
        Else
            polyFunc.MessageAlert(ParentPage, "Error Sending Policy!")
        End If

        dm = Nothing
        polyFunc = Nothing
    End Sub

#End Region

#Region "Fill Details"

    Private Sub Load_CInfo()
        Dim vo As New DAOMain.VW_POLICY_CLIENT_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_CLIENT_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Dim dt As DataTable = dao.Search(vo)

        CInfo_dg.DataSource = dt
        CInfo_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Bene_Dtl()
        Dim vo As New DAOMain.VW_POLICY_BENE_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_BENE_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Beneficiary_dg.DataSource = dao.Search(vo)
        Beneficiary_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Trustee_Dtl()
        Dim vo As New DAOMain.VW_POLICY_TRUSTEES_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_TRUSTEES_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Trustee_dg.DataSource = dao.Search(vo)
        Trustee_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Rider_Dtl()
        Dim vo As New DAOMain.VW_POLICY_PREM_RIDER_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_PREM_RIDER_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Rider_dg.DataSource = dao.Search(vo)
        Rider_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Fund_Dtl()
        Dim vo As New DAOMain.VW_POLICY_FUND_MSTR_VO
        Dim dao As New DAOMain.VW_POLICY_FUND_MSTR_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Fund_dg.DataSource = dao.Search(vo)
        Fund_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Adv_Dtl()
        Dim vo As New DAOMain.VW_POLICY_ADV_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_ADV_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Advisor_dg.DataSource = dao.Search(vo)
        Advisor_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    'Protected Sub Advisor_dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Advisor_dg.ItemDataBound
    '    Select Case e.Item.ItemType
    '        Case ListItemType.Item, ListItemType.AlternatingItem
    '            Dim c As New CIMSfunc
    '            e.Item.Cells(2).Text = c.ParseGetDate(CDate(e.Item.Cells(2).Text))
    '            e.Item.Cells(3).Text = c.ParseGetDate(CDate(e.Item.Cells(3).Text))
    '            c = Nothing
    '    End Select
    'End Sub

    Private Sub Load_Issue_Dtl()
        Dim vo As New DAOMain.VW_POLICY_ISSUE_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_ISSUE_DTL_DAO

        vo.POLICY_SEQ_CD = lblID.Text
        Issue_dg.DataSource = dao.Search(vo)
        Issue_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    'Protected Sub Issue_dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Issue_dg.ItemDataBound
    '    If e.Item.ItemType = ListItemType.Item Then
    '        Dim c As New CIMSfunc
    '        e.Item.Cells(3).Text = c.ParseGetDate(Format(CDate(e.Item.Cells(3).Text), "MM/dd/yyyy"))
    '        c = Nothing
    '    End If
    'End Sub

    Private Sub Load_Referrals()
        Dim vo As New DAOMain.VW_POLICY_REF_DTL_VO
        Dim dao As New DAOMain.VW_POLICY_REF_DTL_DAO

        vo.POLICY_SEQ_NO = lblID.Text
        Referral_dg.DataSource = dao.Search(vo)
        Referral_dg.DataBind()

        vo = Nothing
        dao = Nothing
    End Sub

    Private Sub Load_Payments()
        Dim dao As New DAOMain.VW_PAYMENT_SEARCH_DAO
        Dim vo As New DAOMain.VW_PAYMENT_SEARCH_VO

        With vo
            .POLICY_SEQ_CD = lblID.Text
            .PAY_TAG = 1
        End With

        Dim dt As DataTable = dao.Search(vo)

        With Payment_dg
            .DataSource = dt
            .DataBind()
        End With

        dao = Nothing
        vo = Nothing
    End Sub

    'Protected Sub Payment_dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Payment_dg.ItemDataBound
    '    Select Case e.Item.ItemType
    '        Case ListItemType.Item, ListItemType.AlternatingItem
    '            With e.Item
    '                .Cells(5).Text = Format(CDate(.Cells(5).Text), "M/d/yyyy")
    '                .Cells(6).Text = Format(CInt(.Cells(6).Text), "###,###,##0.00")
    '            End With
    '    End Select
    'End Sub

    'Protected Sub Rider_dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Rider_dg.ItemDataBound
    '    Select Case e.Item.ItemType
    '        Case ListItemType.Item, ListItemType.AlternatingItem
    '            With e.Item
    '                .Cells(2).Text = Format(CInt(.Cells(2).Text), "###,###,##0.00")
    '            End With
    '    End Select
    'End Sub

    Private Sub Load_Attachments()
        Dim dao As New DAOMain.POLICY_TRANS_ATTACH_DTL_DAO
        Dim vo As New DAOMain.POLICY_TRANS_ATTACH_DTL_VO

        vo.POLICY_SEQ_CD = lblID.Text
        Dim dt As DataTable = dao.Search(vo)

        With Attachment_dg
            .DataSource = dt
            .DataBind()
        End With

        dao = Nothing
        vo = Nothing
    End Sub

#End Region

#Region "Detail Navigartion"

#Region "Beneficiary"
    Protected Sub AddBeneficiary_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddBeneficiary_btn.Click

        Session.Add("CName", tdName.InnerText)

        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "beneficiary")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Beneficiary.aspx")
    End Sub

    Protected Sub Beneficiary_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Beneficiary_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)

                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "beneficiary")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Beneficiary.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_BENE_DTL_VO
                Dim dao As New DAOMain.POLICY_BENE_DTL_DAO

                vo.POLICY_BENE_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                dao.Delete(vo)

                Call LoadPolicyInfo()
                Call GetBeneficiaryTotals()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Client Info"
    Protected Sub AddNewBusiness_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddNewBusiness.Click
        Session.Add("Seq_CD", ViewState("Seq_CD"))

        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_CInfo.aspx")
    End Sub

    Protected Sub CInfo_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles CInfo_dg.ItemCommand
        Select Case e.CommandName
            Case "View"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))

                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_CInfo.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_CLIENT_DTL_VO_MAIN
                Dim dao As New DAOMain.POLICY_CLIENT_DTL_DAO_MAIN

                'Check  Attachments
                Dim voAttach As New DAOMain.POLICY_CLIENT_ATTACH_VO
                Dim daoAttach As New DAOMain.POLICY_CLIENT_ATTACH_DAO

                voAttach.POLICY_CLIENT_CD = e.Item.Cells(0).Text
                voAttach.POLICY_SEQ_CD = ViewState("Seq_CD")

                Dim dt As DataTable = daoAttach.Search(voAttach)

                If dt.Rows.Count > 0 Then
                    Dim polyFunc As New PolyFunc.ASPFunc
                    polyFunc.MessageAlert(ParentPage, "Cannot Delete Record! There are existing details!")
                    polyFunc = Nothing
                Else
                    Dim voCor As New DAOMain.POLICY_CLIENT_CORRES_VO
                    Dim daoCor As New DAOMain.POLICY_CLIENT_CORRES_DAO

                    voCor.POLICY_CLIENT_CD = e.Item.Cells(0).Text
                    voCor.POLICY_SEQ_CD = ViewState("Seq_CD")
                    daoCor.Delete(voCor)

                    Dim voRisk As New DAOMain.POLICY_CLIENT_RISK_VO
                    Dim daoRisk As New DAOMain.POLICY_CLIENT_RISK_DAO

                    voRisk.POLICY_CLIENT_CD = e.Item.Cells(0).Text
                    voRisk.POLICY_SEQ_CD = ViewState("Seq_CD")
                    daoRisk.Delete(voRisk)

                    vo.POLICY_CLIENT_CD = e.Item.Cells(0).Text
                    vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                    dao.Delete(vo)

                    Call LoadPolicyInfo()

                    voRisk = Nothing
                    daoRisk = Nothing
                    voCor = Nothing
                    daoCor = Nothing
                End If

                voAttach = Nothing
                daoAttach = Nothing
                vo = Nothing
                dao = Nothing

        End Select
    End Sub

    Protected Sub CInfo_dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles CInfo_dg.ItemDataBound
        Dim chInsured As CheckBox = e.Item.Cells(9).FindControl("Insured_ch")
        Dim chOwner As CheckBox = e.Item.Cells(10).FindControl("Owner_ch")

        If Not IsNothing(chInsured) Then
            If e.Item.Cells(11).Text = 1 Then chInsured.Checked = True
        End If

        If Not IsNothing(chOwner) Then
            If e.Item.Cells(12).Text = 1 Then chOwner.Checked = True
        End If

    End Sub
#End Region

#Region "Trustee"
    Protected Sub AddTrustee_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddTrustee_btn.Click
        Session.Add("CName", tdName.InnerText)

        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "trustee")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Trustee.aspx")
    End Sub

    Protected Sub Trustee_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Trustee_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)

                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "trustee")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Trustee.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_TRUSTEES_DTL_VO
                Dim dao As New DAOMain.POLICY_TRUSTEES_DTL_DAO

                vo.POLICY_TRUSTEES_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                dao.Delete(vo)

                Call LoadPolicyInfo()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Premium Rider"

    Protected Sub AddRider_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRider_btn.Click

        Session.Add("CName", tdName.InnerText)
        Session.Add("Seq_CD", ViewState("Seq_CD"))

        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "rider")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Rider.aspx")
    End Sub

    Protected Sub Rider_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Rider_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)

                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "rider")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Rider.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_PREM_RIDER_DTL_VO
                Dim dao As New DAOMain.POLICY_PREM_RIDER_DTL_DAO

                vo.PREM_RIDER_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                dao.Delete(vo)

                Dim dm As New DAOMain.CommonDAO
                dm.AddParameter(ViewState("Seq_CD"))
                dm.ExecCmdProc("sp_set_premium_amt")
                dm = Nothing

                Call LoadPolicyInfo()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Fund"
    Protected Sub AddFund_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFund_btn.Click
        Session.Add("CName", tdName.InnerText)

        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "fund")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Fund.aspx")
    End Sub

    Protected Sub Fund_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Fund_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)

                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "fund")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Fund.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_FUND_DTL_VO
                Dim dao As New DAOMain.POLICY_FUND_DTL_DAO

                vo.FUND_CD = e.Item.Cells(0).Text
                vo.PRDT_CD = e.Item.Cells(1).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                dao.Delete(vo)

                Call LoadPolicyInfo()
                Call GetFundTotals()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Advisor"

    Protected Sub AddAdvisor_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddAdvisor_btn.Click
        Session.Add("CName", tdName.InnerText)

        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "advisor")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Advisor.aspx")
    End Sub

    Protected Sub Advisor_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Advisor_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Dim cf As New CIMSfunc

                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)

                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("SDate", e.Item.Cells(2).Text)
                Session.Add("ViewTag", "advisor")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Advisor.aspx")

                cf = Nothing
            Case "Delete"
                Dim vo As New DAOMain.POLICY_ADV_DTL_VO
                Dim dao As New DAOMain.POLICY_ADV_DTL_DAO
                Dim cf As New CIMSfunc

                vo.ADVISOR_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                vo.POLICY_ADV_START_DT = cf.ParseSetDate(e.Item.Cells(2).Text)
                dao.Delete(vo)

                Call LoadPolicyInfo()

                cf = Nothing
                vo = Nothing
                dao = Nothing
        End Select
    End Sub

#End Region

#Region "Issues"
    Protected Sub AddIssue_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddIssue_btn.Click
        Session.Add("CName", tdName.InnerText)
        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "issue")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_CIssue.aspx")
    End Sub

    Protected Sub Issue_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Issue_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)
                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "issue")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_CIssue.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_ISSUE_DTL_VO
                Dim dao As New DAOMain.POLICY_ISSUE_DTL_DAO

                vo.POLICY_ISSUE_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_CD = ViewState("Seq_CD")
                dao.Delete(vo)

                Call LoadPolicyInfo()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Referral"

    Protected Sub AddReferral_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddReferral_btn.Click
        Session.Add("CName", tdName.InnerText)
        Session.Add("Seq_CD", ViewState("Seq_CD"))
        Session.Add("PolicyNo", ViewState("PolicyNo"))
        Session.Add("Provider", ViewState("Provider"))
        Session.Add("ViewTag", "referral")
        Session.Add("mode", ViewState("mode"))

        Response.Redirect("~/" & strPath & "/Add_Referral.aspx")
    End Sub

    Protected Sub Referral_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Referral_dg.ItemCommand
        Select Case e.CommandName
            Case "View", "Edit"
                Session.Add("Seq_CD", ViewState("Seq_CD"))
                Session.Add("CName", tdName.InnerText)
                Session.Add("PolicyNo", ViewState("PolicyNo"))
                Session.Add("Provider", ViewState("Provider"))
                Session.Add("CID", e.Item.Cells(0).Text)
                Session.Add("ViewTag", "referral")
                Session.Add("mode", ViewState("mode"))

                Response.Redirect("~/" & strPath & "/Add_Referral.aspx")
            Case "Delete"
                Dim vo As New DAOMain.POLICY_REF_DTL_VO
                Dim dao As New DAOMain.POLICY_REF_DTL_DAO

                vo.REFERRAL_CD = e.Item.Cells(0).Text
                vo.POLICY_SEQ_NO = ViewState("Seq_CD")
                dao.Delete(vo)

                Call LoadPolicyInfo()

                vo = Nothing
                dao = Nothing
        End Select
    End Sub
#End Region

#Region "Payment"
    Protected Sub Payment_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Payment_dg.ItemCommand
        If e.CommandName = "View" Then
            Session.Add("Seq_CD", e.Item.Cells(0).Text)
            Session.Add("CID", e.Item.Cells(1).Text)
            Session.Add("CName", e.Item.Cells(2).Text)
            If Session("CName") = "&nbsp;" Then Session.Remove("CName")
            Session.Add("mode", "view")
            Session.Add("ViewTag", "payment")
            Session.Add("pagesource", Request.FilePath)

            Response.Redirect("~/Accounting/AddPayment.aspx")
        End If
    End Sub
#End Region

#Region "Attachments"
    Protected Sub Attach_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Attach_btn.Click
        Dim dao As New DAOMain.POLICY_TRANS_ATTACH_DTL_DAO
        Dim vo As New DAOMain.POLICY_TRANS_ATTACH_DTL_VO
        Dim cFunc As New PolyFunc.ASPFunc
        Dim cf As New CIMSfunc

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")

            .POLICY_ATTACH_CD = cf.GetLastID("POLICY_ATTACH_CD", "POLICY_TRANS_ATTACH_DTL")
            .FILENAME = SaveFileName(vo)
            .CAPTION = Caption_txt.Text
            .POLICY_ATTACH_CREA_BY = Session("UserName")
            .POLICY_ATTACH_CREA_DT = Now

            If dao.Create_Record(vo) Then
                cFunc.MessageAlert(ParentPage, "File Saved!")
            Else
                cFunc.MessageAlert(ParentPage, "Error Saving File!")
            End If

        End With

        Call Load_Attachments()

        dao = Nothing
        vo = Nothing
        cFunc = Nothing
    End Sub

    Protected Sub Attachment_dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles Attachment_dg.ItemCommand
        Select Case e.CommandName
            Case "View"
                Response.Redirect(e.Item.Cells(1).Text)
            Case "Delete"
                Dim dao As New DAOMain.POLICY_TRANS_ATTACH_DTL_DAO
                Dim vo As New DAOMain.POLICY_TRANS_ATTACH_DTL_VO

                vo.POLICY_SEQ_CD = lblID.Text
                vo.POLICY_ATTACH_CD = e.Item.Cells(0).Text

                If dao.Delete(vo) Then
                    File.Delete(MapPath(e.Item.Cells(1).Text))
                End If

                Call Load_Attachments()
                dao = Nothing
                vo = Nothing
        End Select
    End Sub

    Private Function SaveFileName(ByVal vo As DAOMain.POLICY_TRANS_ATTACH_DTL_VO) As String
        Dim ReturnFile As String = ""
        Dim FileExtn As String = GetFileExtension(FileUpload1.FileName)

        Dim strFile As String = "policy_" & _
                    vo.POLICY_SEQ_CD & "_" & vo.POLICY_ATTACH_CD & FileExtn

        If File.Exists(strFile) Then File.Delete(strFile)
        FileUpload1.SaveAs(Server.MapPath(Request.ApplicationPath & "/File_Attach/" & strFile))
        ReturnFile = Request.ApplicationPath & "/File_Attach/" & strFile

        Return ReturnFile
    End Function

    Private Function GetFileExtension(ByVal FileName As String) As String
        Dim strReturn As String = ""

        Dim i As Integer

        For i = 1 To Len(FileName)
            If Mid(FileName, i, 1) = "." Then
                strReturn = Mid(FileName, i, Len(FileName))
            End If
        Next

        Return strReturn
    End Function
#End Region

#End Region

#Region "Detail Datagrid Navigation"
    Private Sub DetailLinkClick()
        Client_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Riders_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Funds_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Beneficiaries_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Trustee_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Advisor_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Issues_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Referrals_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Payment_btn.ForeColor = Drawing.Color.FromName("#0C58A2")
        Attachment_btn.ForeColor = Drawing.Color.FromName("#0C58A2")


        Client_div.Visible = False
        Riders_div.Visible = False
        Funds_div.Visible = False
        Beneficiaries_div.Visible = False
        Trustees_div.Visible = False
        Advisors_div.Visible = False
        Issues_div.Visible = False
        Referral_div.Visible = False
        Payment_div.Visible = False
        Attachment_div.Visible = False

        Attach_panel.Visible = False

        If IsNothing(Session("ViewTag")) Then
            Client_div.Visible = True
            Client_btn.ForeColor = Drawing.Color.Black
        Else
            Select Case Session("ViewTag")

                Case "rider"
                    Riders_div.Visible = True
                    Riders_btn.ForeColor = Drawing.Color.Black
                Case "fund"
                    Funds_div.Visible = True
                    Funds_btn.ForeColor = Drawing.Color.Black
                Case "beneficiary"
                    Beneficiaries_div.Visible = True
                    Beneficiaries_btn.ForeColor = Drawing.Color.Black
                Case "trustee"
                    Trustees_div.Visible = True
                    Trustee_btn.ForeColor = Drawing.Color.Black
                Case "advisor"
                    Advisors_div.Visible = True
                    Advisor_btn.ForeColor = Drawing.Color.Black
                Case "issue"
                    Issues_div.Visible = True
                    Issues_btn.ForeColor = Drawing.Color.Black
                Case "referral"
                    Referral_div.Visible = True
                    Referrals_btn.ForeColor = Drawing.Color.Black
                Case "payment"
                    Payment_div.Visible = True
                    Payment_btn.ForeColor = Drawing.Color.Black
                Case "attachment"
                    Attachment_div.Visible = True
                    Attachment_btn.ForeColor = Drawing.Color.Black
                    Attach_panel.Visible = True
            End Select
        End If


        Session.Remove("ViewTag")
    End Sub

    Protected Sub Client_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Client_btn.Click
        Session.Remove("ViewTag")
        DetailLinkClick()
    End Sub

    Protected Sub Riders_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Riders_btn.Click
        Session.Add("ViewTag", "rider")
        DetailLinkClick()
    End Sub

    Protected Sub Funds_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Funds_btn.Click
        Session.Add("ViewTag", "fund")
        DetailLinkClick()
    End Sub

    Protected Sub Beneficiaries_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Beneficiaries_btn.Click
        Session.Add("ViewTag", "beneficiary")
        DetailLinkClick()
    End Sub

    Protected Sub Trustee_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Trustee_btn.Click
        Session.Add("ViewTag", "trustee")
        DetailLinkClick()
    End Sub

    Protected Sub Advisor_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Advisor_btn.Click
        Session.Add("ViewTag", "advisor")
        DetailLinkClick()
    End Sub

    Protected Sub Issues_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Issues_btn.Click
        Session.Add("ViewTag", "issue")
        DetailLinkClick()
    End Sub

    Protected Sub Referrals_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Referrals_btn.Click
        Session.Add("ViewTag", "referral")
        DetailLinkClick()
    End Sub

    Protected Sub Payment_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Payment_btn.Click
        Session.Add("ViewTag", "payment")
        DetailLinkClick()
    End Sub

    Protected Sub Attachment_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Attachment_btn.Click
        Session.Add("ViewTag", "attachment")
        DetailLinkClick()

    End Sub
#End Region

#Region "Datagrid Events"
    Protected Sub Beneficiary_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Beneficiary_dg.PageIndexChanged
        Beneficiary_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Bene_Dtl()
    End Sub

    Protected Sub CInfo_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles CInfo_dg.PageIndexChanged
        CInfo_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_CInfo()
    End Sub

    Protected Sub Trustee_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Trustee_dg.PageIndexChanged
        Trustee_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Trustee_Dtl()
    End Sub

    Protected Sub Rider_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Rider_dg.PageIndexChanged
        Rider_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Rider_Dtl()
    End Sub

    Protected Sub Fund_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Fund_dg.PageIndexChanged
        Fund_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Fund_Dtl()
    End Sub

    Protected Sub Advisor_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Advisor_dg.PageIndexChanged
        Advisor_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Adv_Dtl()
    End Sub

    Protected Sub Issue_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Issue_dg.PageIndexChanged
        Issue_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Issue_Dtl()
    End Sub

    Protected Sub Referral_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Referral_dg.PageIndexChanged
        Referral_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Referrals()
    End Sub

    Protected Sub Payment_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Payment_dg.PageIndexChanged
        Payment_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Payments()
    End Sub

    Protected Sub Attachment_dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles Attachment_dg.PageIndexChanged
        Attachment_dg.CurrentPageIndex = e.NewPageIndex
        Call Load_Attachments()
    End Sub
#End Region


End Class
